package cn.edu.sgu.www.mhxysy.service.potency.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.potency.PotencyFruitPatternCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.potency.PotencyFruitPatternCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.potency.PotencyFruitPatternCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.pager.potency.PotencyPatternPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.potency.PotencyFruitPatternCategoryService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class PotencyFruitPatternCategoryServiceImpl implements PotencyFruitPatternCategoryService {

    private final PotencyFruitPatternCategoryMapper potencyFruitPatternCategoryMapper;

    @Autowired
    public PotencyFruitPatternCategoryServiceImpl(PotencyFruitPatternCategoryMapper potencyFruitPatternCategoryMapper) {
        this.potencyFruitPatternCategoryMapper = potencyFruitPatternCategoryMapper;
    }

    @Override
    public void insert(PotencyFruitPatternCategoryDTO potencyFruitPatternCategoryDTO) {
        PotencyFruitPatternCategory potencyFruitPatternCategory = toEntity(potencyFruitPatternCategoryDTO);

        potencyFruitPatternCategory.setId(null);

        potencyFruitPatternCategoryMapper.insert(potencyFruitPatternCategory);
    }

    @Override
    public void deleteById(Integer id) {
        potencyFruitPatternCategoryMapper.deleteById(id);
    }

    @Override
    public void updateById(PotencyFruitPatternCategoryDTO potencyFruitPatternCategoryDTO) {
        ObjectUtils.requireNonNull(potencyFruitPatternCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

        PotencyFruitPatternCategory potencyFruitPatternCategory = toEntity(potencyFruitPatternCategoryDTO);

        potencyFruitPatternCategoryMapper.updateById(potencyFruitPatternCategory);
    }

    @Override
    public List<PotencyFruitPatternCategory> selectAll() {
        return potencyFruitPatternCategoryMapper.selectList(null);
    }

    @Override
    public PotencyFruitPatternCategory selectById(Integer id) {
        return potencyFruitPatternCategoryMapper.selectById(id);
    }

    @Override
    public List<PotencyFruitPatternCategory> selectByType(Integer type) {
        QueryWrapper<PotencyFruitPatternCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("type", type);

        return potencyFruitPatternCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public Page<PotencyFruitPatternCategory> selectByPage(PotencyPatternPager pager) {
        QueryWrapper<PotencyFruitPatternCategory> queryWrapper = new QueryWrapper<>();
        Page<PotencyFruitPatternCategory> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getType() != null,
                "type", pager.getType()
        );

        return potencyFruitPatternCategoryMapper.selectPage(page, queryWrapper);
    }

    private PotencyFruitPatternCategory toEntity(PotencyFruitPatternCategoryDTO patternEffectDTO) {
        Integer id = patternEffectDTO.getId();
        String name = patternEffectDTO.getName();

        // 验证重复
        QueryWrapper<PotencyFruitPatternCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.ne(id != null, "id", id);
        queryWrapper.eq("name", name);

        Long count = potencyFruitPatternCategoryMapper.selectCount(queryWrapper);

        if (count > 0) {
            throw new GlobalException(ResponseCode.CONFLICT, "保存失败，数据重复！");
        }

        // 封装并返回实体类
        PotencyFruitPatternCategory potencyFruitPatternCategory = new PotencyFruitPatternCategory();

        potencyFruitPatternCategory.setId(id);
        potencyFruitPatternCategory.setName(name);
        potencyFruitPatternCategory.setType(patternEffectDTO.getType());
        potencyFruitPatternCategory.setComboEffect(patternEffectDTO.getComboEffect());

        return potencyFruitPatternCategory;
    }

}